import { getStoreDetail } from '@mall/api/store'
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import router from "@src/router";
import mall from "@src/store/mall";
import base from "@src/store/base";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['isiPhoneX', 'themeColor', 'themeTitleColor']
  }, {
    store: mall,
    actions: ['initLocation', 'setStoreId']
  }],
  data: {
    dataObj: null,
    bannerListConfig: {
      indicatorDots: false,
      autoplay: false,
      interval: 4000,
      duration: 500
    },
    currentIndex: 1,
    indexSum: null,
    //距离我的距离
    distance: 0,
    imgheights: [],
    storeId: null
  },
  methods: {
    onLoad: function (options) {
      let storeId = options.storeId
      this.setData({ storeId: storeId })
    },
    onLogin() {
      this.initLocation().then(res => {
        this.getStoreDetailFunc(res.latitude, res.longitude);
      }).catch(() => {
        this.getStoreDetailFunc()
      })
    },
    getStoreDetailFunc(latitude, longitude) {
      let data = { storeId: this.data.storeId };
      if (latitude) {
        data.latitude = latitude
        data.longitude = longitude
      }
      getStoreDetail(data).then(res => {
        if (res.code === 0) {
          this.setData({
            dataObj: res.data,
            indexSum: res.data.imageList.length
          })
        }
      })
    },
    //导航
    goLocation() {
      let dataObj = this.data.dataObj;
      wx.openLocation({
        name: dataObj.name,
        address: dataObj.address,
        latitude: dataObj.latitude,
        longitude: dataObj.longitude,
        scale: 18
      })
    },

    //获取图片的真实高度
    imageLoad: function (e) {//获取图片真实宽度

      let imgwidth = e.detail.width;
      let imgheight = e.detail.height;
      //宽高比
      let ratio = imgwidth / imgheight;

      //计算的高度值
      let viewHeight = 750 / ratio;
      let imgheight_two = viewHeight;
      let imgheights = this.data.imgheights;
      //把每一张图片的对应的高度记录到数组里
      imgheights[e.target.dataset.id] = imgheight_two;
      this.setData({
        imgheights: imgheights
      })
    },

    //去购物
    goShop() {
      this.setStoreId(this.data.storeId)
      router.push({ name: 'mall/goods-list', params: { storeId: this.data.storeId } })
    },
    //调起手机拨号功能
    telphone(e) {
      //console.log('e', e)
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.phone, //模拟测试
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
  }
})
